/* eslint global-require: 0, flowtype-errors/show-errors: 0 */

/**
 * This module executes inside of electron's main process. You can start
 * electron renderer process from here and communicate with the other processes
 * through IPC.
 *
 * When running `npm run build` or `npm run build-main`, this file is compiled to
 * `./app/main.prod.js` using webpack. This gives us some performance wins.
 *
 * @flow
 */
import {
  app,
  BrowserWindow
} from 'electron';
// import MenuBuilder from './menu';
import { ipcMain } from 'electron';

let mainWindow = null;

if (process.env.NODE_ENV === 'production') {
  const sourceMapSupport = require('source-map-support');
  sourceMapSupport.install();
}

if (process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true') {
  require('electron-debug')();
  const path = require('path');
  const p = path.join(__dirname, '..', 'app', 'node_modules');
  require('module').globalPaths.push(p);
}

const installExtensions = async () => {
  const installer = require('electron-devtools-installer');
  const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
  const extensions = [
    'REACT_DEVELOPER_TOOLS',
    'REDUX_DEVTOOLS'
  ];

  return Promise
    .all(extensions.map(name => installer.default(installer[name], forceDownload)))
    .catch(console.log);
};

/**
 * Add event listeners...
 */

app.on('window-all-closed', () => {
  // Respect the OSX convention of having the application in memory even
  // after all windows have been closed
  if (process.platform !== 'darwin') {
    app.quit();
  }
});


// 登录窗口最小化
ipcMain.on('window-min', () => {
  mainWindow.minimize();
});

// 登录窗口最大化
ipcMain.on('window-max', () => {
  if (mainWindow.isMaximized()) {
    mainWindow.restore();
  } else {
    mainWindow.maximize();
  }
});

// 窗口关闭
ipcMain.on('window-close', () => {
  mainWindow.close();
});

// 窗口重载
ipcMain.on('window-reload', () => {
  mainWindow.reload();
});

const windowWidth = 440;
const windowHeight = 420;
// 这里一定不用能用常量const 
let permissionCode = 1;
const isResizable = false;
const isMaximizable = false;

// 设置登录前数据
ipcMain.on('getBeforeLoginData', (event) => {
  event.sender.send('returnBeforeLoginData', permissionCode);
});

// 设置登录后数据
// 登录后 设置窗口宽度和高度
ipcMain.on('getAfterLoginData', (event, code) => {
  console.log(code);
  permissionCode = code
  mainWindow.setSize(1024, 728);
  mainWindow.setResizable(true);
  mainWindow.isResizable(true);
  mainWindow.isMaximizable(true);
  event.sender.send('returnAfterLoginData', permissionCode);
});

app.on('ready', async () => {
  if (process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true') {
    await installExtensions();
  }

  mainWindow = new BrowserWindow({
    show: false,
    width: windowWidth,
    height: windowHeight,
    frame: false,
    transparent: false,
    backgroundColor: '#333C4E',
    resizable: isResizable,
    maximizable: isMaximizable,

  });

  // mainWindow.setTitle('123');
  // 设置窗口图标
  // mainWindow.setIcon(icon)

  mainWindow.loadURL(`file://${__dirname}/app.html`);

  // @TODO: Use 'ready-to-show' event
  //        https://github.com/electron/electron/blob/master/docs/api/browser-window.md#using-ready-to-show-event
  mainWindow.webContents.on('did-finish-load', () => {
    if (!mainWindow) {
      throw new Error('"mainWindow" is not defined');
    }
    mainWindow.show();
    mainWindow.focus();
  });

  mainWindow.on('closed', () => {
    mainWindow = null;
  });

  // const menuBuilder = new MenuBuilder(mainWindow);
  // menuBuilder.buildMenu();
  mainWindow.once('ready-to-show', () => {
    mainWindow.show();
  });

  // const child = new BrowserWindow({
  // parent: mainWindow,
  //   modal: true,
  //   show: false,
  //     frame: false, });
  // child.loadURL('https://github.com');
  // child.once('ready-to-show', () => {
  //   child.show();
  // });
});
